/**
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: gzhc365 </p>
 */
package cn.midi365.train.controller.filter;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eteam.common.entity.TeamContext;
import com.eteam.common.helper.TeamContextHelper;
import com.eteam.common.utils.JsonRespTool;
import com.eteam.common.utils.Utils;

/**
 * Description: 营销小程序过滤器
 * 
 * @author zihong
 * @date: 2018年4月10日 下午2:38:37
 * @version 1.0
 * @since JDK 1.7
 */
public class WeddingSessionFilter implements Filter {

    private static Logger logger = LoggerFactory.getLogger(WeddingSessionFilter.class);

    private String[] oauthUrl = { "/qy/api/oauth" };


    /**
     *
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        logger.info("-------------------doFilter Request started:---------------------");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();

        // 获取请求的Url
        logger.info("访问链接地址：{} 参数: {} {} ", request.getRequestURL() + "?" + request.getQueryString(),
                JSONObject.toJSONString(request.getParameterMap()));
        String requestURI = request.getRequestURI();
        String encodedUrl = request.getParameter("url");
        try {

            filterChain.doFilter(request, response);
        } catch (Exception e) {
            logger.error("参数异常：e:{}", e);
            JsonRespTool.writeJson(response, "{\"code\":-1,\"msg\":\"参数异常!\"}");
            return;
        }
    }

    private void filter(HttpServletRequest request, HttpServletResponse response, TeamContext context, HttpSession session,
            String encodedUrl) throws IOException {
        String corpIdPar;
        String agentIdPar;
        String suiteIdPar;
        String url = URLDecoder.decode(encodedUrl, "utf-8");
        logger.info("sequence:{} 实际请求的url : {}", context, url);
        corpIdPar = request.getParameter("corpId");
        suiteIdPar = request.getParameter("suiteId");
        agentIdPar = request.getParameter("agentId");
        String localUrl = request.getRequestURL().toString();
        // 处理https协议
        if (StringUtils.startsWith(url, "https")) {
            localUrl = StringUtils.replace(localUrl, "http", "https", 1);
        }
        String queryString = request.getQueryString();
        // 第个参数之后为业务数据
        String businessData = request.getParameter("businessData");

        logger.info("sequence:{} businessData:{} ", context, businessData);
        if (StringUtils.isNotBlank(queryString)) {
            // 请求参数不为空的
            localUrl = localUrl + "?" + queryString;
        }
        String usUrl = URLEncoder.encode(localUrl, "utf-8");
        String code = request.getParameter("code");
        String userId = (String) session.getAttribute("userId");
        if (!Utils.isEmpty(userId)) {
            response.sendRedirect(url);
            return;
        } else {
            if (StringUtils.isBlank(code)) {
                logger.info("微信code为：{}", code);
                String getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + corpIdPar + "&redirect_uri="
                        + usUrl + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
                logger.info("获取code的url--->{}", getCodeUrl);
                response.sendRedirect(getCodeUrl);
                return;
            } else {

            }
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
